use crate::{components, dao};
use crate::models::models::components::v2ray::basic_setting::ComponentV2rayBasicSetting;
use crate::models::models::components::v2ray::info::{ComponentV2rayInfo, ComponentV2rayState};
use crate::runtime::response::MagicBoxResponse;
use crate::runtime::result::MagicBoxResult;

#[tauri::command(rename_all = "snake_case")]
pub(crate) async fn invoke_component_v2ray_info() -> MagicBoxResult<MagicBoxResponse<ComponentV2rayInfo>> {
    let mut info = ComponentV2rayInfo::default();
    info.version = "-".to_string();
    info.state = ComponentV2rayState::NotInstall;
    info.basic_setting = ComponentV2rayBasicSetting::default();

    if ComponentV2rayState::NotInstall == components::v2ray::manager::manager().state()? {
        return Ok(MagicBoxResponse::new_success(info));
    }

    info.version = components::v2ray::manager::manager().version_name()?;
    info.state = components::v2ray::manager::manager().state()?;
    info.install_stage = components::v2ray::manager::manager().get_install_stage();
    info.install_percent = components::v2ray::manager::manager().get_install_percent();

    let basic_setting = dao::components::v2ray::setting::get_basic_setting();
    if basic_setting.is_none() {
        return Ok(MagicBoxResponse::new_success(info));
    }

    info.basic_setting = basic_setting.unwrap();

    Ok(MagicBoxResponse::new_success(info))
}